<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>JS篇 | 李玉明</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="一道常被人轻视的前端JS面试题标签（空格分隔）： JavaScript

1234567891011121314151617181920212223242526function Foo() &amp;#123;getName = function () &amp;#123; alert (1); &amp;#125;;return this;&amp;#125;Foo.getName = function () &amp;#123;al">
<meta property="og:type" content="article">
<meta property="og:title" content="JS篇">
<meta property="og:url" content="http://yoursite.com/2016/05/27/一道常被人轻视的前端JS面试题/index.html">
<meta property="og:site_name" content="李玉明">
<meta property="og:description" content="一道常被人轻视的前端JS面试题标签（空格分隔）： JavaScript

1234567891011121314151617181920212223242526function Foo() &amp;#123;getName = function () &amp;#123; alert (1); &amp;#125;;return this;&amp;#125;Foo.getName = function () &amp;#123;al">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/746158/201602/746158-20160214172948591-1509302580.png">
<meta property="og:updated_time" content="2016-05-28T01:07:05.296Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JS篇">
<meta name="twitter:description" content="一道常被人轻视的前端JS面试题标签（空格分隔）： JavaScript

1234567891011121314151617181920212223242526function Foo() &amp;#123;getName = function () &amp;#123; alert (1); &amp;#125;;return this;&amp;#125;Foo.getName = function () &amp;#123;al">
<meta name="twitter:image" content="http://images2015.cnblogs.com/blog/746158/201602/746158-20160214172948591-1509302580.png">
  
    <link rel="alternative" href="/atom.xml" title="李玉明" type="application/atom+xml">
  
  
    <link rel="icon" href="/img/favicon.png">
  
  
  <link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.5.0/animate.min.css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/font-awesome/css/font-awesome.min.css">
  <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  
  
      <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  
  <!-- 加载特效 -->
    <script src="/js/pace.js"></script>
    <link href="/css/pace/pace-theme-flash.css" rel="stylesheet" />
  <script>
      var yiliaConfig = {
          fancybox: true,
          animate: true,
          isHome: false,
          isPost: true,
          isArchive: false,
          isTag: false,
          isCategory: false,
          open_in_new: false
      }
  </script>
</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            
            <img lazy-src="/img/head.jpg" class="js-avatar">
            
        </a>

        <hgroup>
          <h1 class="header-author"><a href="/">liyuming</a></h1>
        </hgroup>

        
        
        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">博客首页</a></li>
                        
                            <li><a href="/archives">作品展示</a></li>
                        
                            <li><a href="/">随笔</a></li>
                        
                            <li><a href="/archives">静心阅读</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fl mail" target="_blank" href="http://my.csdn.net/github_35151727" title="mail">mail</a>
                            
                                <a class="fl github" target="_blank" href="https://github.com/ljym-246810" title="github">github</a>
                            
                                <a class="fl zhihu" target="_blank" href="https://www.zhihu.com/people/li-yu-ming122" title="zhihu">zhihu</a>
                            
                                <a class="fl weibo" target="_blank" href="http://weibo.com/3888047260" title="weibo">weibo</a>
                            
                                <a class="fl google" target="_blank" href="http://weibo.com/u/5089556680?is_all=1" title="google">google</a>
                            
                                <a class="fl twitter" target="_blank" href="https://wx.qq.com/" title="twitter">twitter</a>
                            
                                <a class="fl facebook" target="_blank" href="http://t.qq.com/liujia1046408090?setTheme=1" title="facebook">facebook</a>
                            
                                <a class="fl rss" target="_blank" href="http://www.renren.com/860433942#nogo" title="rss">rss</a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://MOxFIVE.github.io/">MOxFIVE</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.vsay.cn/">DoubleV</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.ccwebsite.com/">兮兮</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://blog.dandyweng.com/">翁天信</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.plqblog.com/views/index.php">潘利强</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.hankin.cn/">hankin</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://blog.waydrow.com/">waydrow</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://silenceport.com">喻南航</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://hubojing.coding.me/">胡博靖</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://blog.lzkzhiliang.com/">zhiliang</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://weilai5432.github.io/">未来部落</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://blog.qiujinfeng.com/">qiujinfeng</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">编程是一种单调的生活，因此程序员比普通人需要更多的关怀，更多的友情。</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">liyuming</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                
                    <img lazy-src="/img/head.jpg" class="js-avatar">
                
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">liyuming</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">博客首页</a></li>
                
                    <li><a href="/archives">作品展示</a></li>
                
                    <li><a href="/">随笔</a></li>
                
                    <li><a href="/archives">静心阅读</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                <div class="social">
                    
                        <a class="mail" target="_blank" href="http://my.csdn.net/github_35151727" title="mail">mail</a>
                    
                        <a class="github" target="_blank" href="https://github.com/ljym-246810" title="github">github</a>
                    
                        <a class="zhihu" target="_blank" href="https://www.zhihu.com/people/li-yu-ming122" title="zhihu">zhihu</a>
                    
                        <a class="weibo" target="_blank" href="http://weibo.com/3888047260" title="weibo">weibo</a>
                    
                        <a class="google" target="_blank" href="http://weibo.com/u/5089556680?is_all=1" title="google">google</a>
                    
                        <a class="twitter" target="_blank" href="https://wx.qq.com/" title="twitter">twitter</a>
                    
                        <a class="facebook" target="_blank" href="http://t.qq.com/liujia1046408090?setTheme=1" title="facebook">facebook</a>
                    
                        <a class="rss" target="_blank" href="http://www.renren.com/860433942#nogo" title="rss">rss</a>
                    
                </div>
            </nav>
        </header>                
    </div>
</nav>
      <div class="body-wrap"><article id="post-一道常被人轻视的前端JS面试题" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2016/05/27/一道常被人轻视的前端JS面试题/" class="article-date">
      <time datetime="2016-05-27T06:25:30.000Z" itemprop="datePublished">2016-05-27</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JS篇
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <h1 id="一道常被人轻视的前端JS面试题"><a href="#一道常被人轻视的前端JS面试题" class="headerlink" title="一道常被人轻视的前端JS面试题"></a>一道常被人轻视的前端JS面试题</h1><p>标签（空格分隔）： JavaScript</p>
<hr>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">function Foo() &#123;</span><br><span class="line">getName = function () &#123; alert (1); &#125;;</span><br><span class="line">return this;</span><br><span class="line">&#125;</span><br><span class="line">Foo.getName = function () &#123;</span><br><span class="line">alert (2);</span><br><span class="line">&#125;;</span><br><span class="line">Foo.prototype.getName = function () &#123; </span><br><span class="line">alert (3);</span><br><span class="line">&#125;;</span><br><span class="line">var getName = function () &#123;</span><br><span class="line">alert (4);</span><br><span class="line">&#125;;</span><br><span class="line">function getName() &#123; </span><br><span class="line">alert (5);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//请写出以下输出结果：</span><br><span class="line">Foo.getName();</span><br><span class="line">getName();</span><br><span class="line">Foo().getName();</span><br><span class="line">getName();</span><br><span class="line">new Foo.getName();</span><br><span class="line">new Foo().getName();</span><br><span class="line">new new Foo().getName();</span><br></pre></td></tr></table></figure>
<hr>
<p>答案是：</p>
<hr>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Foo.getName();//2</span><br><span class="line">getName();//4</span><br><span class="line">Foo().getName();//1</span><br><span class="line">getName();//1</span><br><span class="line">new Foo.getName();//2</span><br><span class="line">new Foo().getName();//3</span><br><span class="line">new new Foo().getName();//3</span><br></pre></td></tr></table></figure>
<hr>
<p>此题是我综合之前的开发经验以及遇到的JS各种坑汇集而成。此题涉及的知识点众多，包括变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级等等。</p>
<hr>
<h2 id="第一问"><a href="#第一问" class="headerlink" title="第一问"></a><strong>第一问</strong></h2><p>先看此题的上半部分做了什么，首先定义了一个叫Foo的函数，之后为Foo创建了一个叫getName的静态属性存储了一个匿名函数，之后为Foo的原型对象新创建了一个叫getName的匿名函数。之后又通过函数变量表达式创建了一个getName的函数，最后再声明一个叫getName函数。</p>
<p>第一问的 Foo.getName 自然是访问Foo函数上存储的静态属性，自然是2，没什么可说的。</p>
<hr>
<h2 id="第二问"><a href="#第二问" class="headerlink" title="第二问"></a><strong>第二问</strong></h2><p>第二问，直接调用 getName 函数。既然是直接调用那么就是访问当前上文作用域内的叫getName的函数，所以跟1 2 3都没什么关系。此题有无数面试者回答为5。此处有两个坑，一是变量声明提升，二是函数表达式。</p>
<h2 id="变量声明提升"><a href="#变量声明提升" class="headerlink" title="变量声明提升"></a>变量声明提升</h2><p>即所有声明变量或声明函数都会被提升到当前函数的顶部。<br>例如下代码:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">console.log(&apos;x&apos; in window);//true</span><br><span class="line"> var x;</span><br><span class="line"> x = 0;</span><br></pre></td></tr></table></figure></p>
<p>代码执行时js引擎会将声明语句提升至代码最上方，变为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var x;</span><br><span class="line">   console.log(&apos;x&apos; in window);//true</span><br><span class="line">   x = 0;</span><br></pre></td></tr></table></figure></p>
<h2 id="函数表达式"><a href="#函数表达式" class="headerlink" title="函数表达式"></a>函数表达式</h2><p>var getName 与 function getName 都是声明语句，区别在于 var getName 是函数表达式，而 function getName 是函数声明。关于JS中的各种函数创建方式可以看 大部分人都会做错的经典JS闭包面试题 这篇文章有详细说明。</p>
<p>函数表达式最大的问题，在于js会将此代码拆分为两行代码分别执行。</p>
<p>例如下代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">console.log(x);//输出：function x()&#123;&#125;</span><br><span class="line">var x=1;</span><br><span class="line">function x()&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>所以最终函数声明的x覆盖了变量声明的x，log输出为x函数。<br>同理，原题中代码最终执行时的是：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">function Foo() &#123;</span><br><span class="line">    getName = function () &#123; </span><br><span class="line">    alert (1); </span><br><span class="line">    &#125;;</span><br><span class="line">    return this;</span><br><span class="line">    &#125;</span><br><span class="line">    var getName;//只提升变量声明</span><br><span class="line">    function getName() &#123; alert (5);&#125;//提升函数声明，覆盖var的声明</span><br><span class="line">    Foo.getName = function () &#123; </span><br><span class="line">    alert (2);</span><br><span class="line">    &#125;;</span><br><span class="line">    Foo.prototype.getName = function () &#123;</span><br><span class="line">    alert (3);</span><br><span class="line">    &#125;;</span><br><span class="line">    getName = function () &#123; </span><br><span class="line">    alert(4);</span><br><span class="line">    &#125;;//最终的赋值再次覆盖function</span><br><span class="line">     getName声明</span><br><span class="line">     getName();//最终输出4</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="第三问"><a href="#第三问" class="headerlink" title="第三问"></a><strong>第三问</strong></h2><p>第三问的 Foo().getName(); 先执行了Foo函数，然后调用Foo函数的返回值对象的getName属性函数。</p>
<p>Foo函数的第一句  getName = function () { alert (1); };  是一句函数赋值语句，注意它没有var声明，所以先向当前Foo函数作用域内寻找getName变量，没有。再向当前函数作用域上层，即外层作用域内寻找是否含有getName变量，找到了，也就是第二问中的alert(4)函数，将此变量的值赋值为 function(){alert(1)}。</p>
<p><strong>此处实际上是将外层作用域内的getName函数修改了。</strong></p>
<blockquote>
<p>注意：此处若依然没有找到会一直向上查找到window对象，若window对象中也没有getName属性，就在window对象中创建一个getName变量。</p>
</blockquote>
<p>之后Foo函数的返回值是this，而JS的this问题博客园中已经有非常多的文章介绍，这里不再多说。</p>
<p>简单的讲，<strong>this的指向是由所在函数的调用方式决定的</strong>。而此处的直接调用方式，this指向window对象。</p>
<p>遂Foo函数返回的是window对象，相当于执行 window.getName() ，而window中的getName已经被修改为alert(1)，所以最终会输出1</p>
<p>此处考察了两个知识点，一个是变量作用域问题，一个是this指向问题。</p>
<hr>
<h2 id="第四问"><a href="#第四问" class="headerlink" title="第四问"></a><strong>第四问</strong></h2><p>直接调用getName函数，相当于 window.getName() ，因为这个变量已经被Foo函数执行时修改了，遂结果与第三问相同，为1</p>
<hr>
<h2 id="第五问"><a href="#第五问" class="headerlink" title="第五问"></a><strong>第五问</strong></h2><p>第五问 new Foo.getName(); ,此处考察的是js的运算符优先级问题。</p>
<p>js运算符优先级:<br><img src="http://images2015.cnblogs.com/blog/746158/201602/746158-20160214172948591-1509302580.png" alt="此处输入图片的描述"></p>
<p>  参考链接：<br>  <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence" target="_blank" rel="external">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence</a></p>
<p>  通过查上表可以得知点（.）的优先级高于new操作，遂相当于是:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">new (Foo.getName)();</span><br></pre></td></tr></table></figure>
<p>所以实际上将getName函数作为了构造函数来执行，遂弹出2。</p>
<hr>
<h2 id="第六问"><a href="#第六问" class="headerlink" title="第六问"></a><strong>第六问</strong></h2><p>第六问 new Foo().getName() ，首先看运算符优先级括号高于new，实际执行为<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(new Foo()).getName()</span><br></pre></td></tr></table></figure></p>
<p>遂先执行Foo函数，而Foo此时作为构造函数却有返回值，所以这里需要说明下js中的构造函数返回值问题。</p>
<h1 id="构造函数的返回值"><a href="#构造函数的返回值" class="headerlink" title="构造函数的返回值"></a>构造函数的返回值</h1><p>在传统语言中，构造函数不应该有返回值，实际执行的返回值就是此构造函数的实例化对象。</p>
<p>而在js中构造函数可以有返回值也可以没有。<br>1、没有返回值则按照其他语言一样返回实例化对象。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function F()&#123;&#125;</span><br><span class="line">undefined</span><br><span class="line">new F()</span><br><span class="line">F()</span><br></pre></td></tr></table></figure></p>
<hr>
<p>2、若有返回值则检查其返回值是否为引用类型。如果是非引用类型，如基本类型（string,number,boolean,null,undefined）则与无返回值相同，实际返回其实例化对象。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function F()&#123;return true;&#125;</span><br><span class="line">undefined</span><br><span class="line">new F()</span><br><span class="line">F()</span><br></pre></td></tr></table></figure></p>
<hr>
<p>3、若返回值是引用类型，则实际返回值为这个引用类型。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function F()&#123;a:1&#125;</span><br><span class="line">undefined</span><br><span class="line">new F()</span><br><span class="line">F()</span><br></pre></td></tr></table></figure></p>
<p>原题中，返回的是this，而this在构造函数中本来就代表当前实例化对象，遂最终Foo函数返回实例化对象。</p>
<p>之后调用实例化对象的getName函数，因为在Foo构造函数中没有为实例化对象添加任何属性，遂到当前对象的原型对象（prototype）中寻找getName，找到了。</p>
<p>遂最终输出3。</p>
<hr>
<h2 id="第七问"><a href="#第七问" class="headerlink" title="第七问"></a><strong>第七问</strong></h2><p>第七问, new new Foo().getName(); 同样是运算符优先级问题。</p>
<p>最终实际执行为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">new ((new Foo()).getName)();</span><br></pre></td></tr></table></figure></p>
<p>先初始化Foo的实例化对象，然后将其原型上的getName函数作为构造函数再次new。</p>
<p>遂最终结果为3</p>
<hr>
<h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a><strong>最后</strong></h2><p>就答题情况而言，第一问100%都可以回答正确，第二问大概只有50%正确率，第三问能回答正确的就不多了，第四问再正确就非常非常少了。其实此题并没有太多刁钻匪夷所思的用法，都是一些可能会遇到的场景，而大多数人但凡有1年到2年的工作经验都应该完全正确才对。</p>
<p>只能说有一些人太急躁太轻视了，希望大家通过此文了解js一些特性。</p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2016/05/27/一道常被人轻视的前端JS面试题/">JS篇</a></p>
        <p><span>文章作者:</span><a href="/" title="访问 liyuming 的个人博客">liyuming</a></p>
        <p><span>发布时间:</span>2016年05月27日 - 14时25分</p>
        <p><span>最后更新:</span>2016年05月28日 - 09时07分</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2016/05/27/一道常被人轻视的前端JS面试题/" title="JS篇">http://yoursite.com/2016/05/27/一道常被人轻视的前端JS面试题/</a>
            <span class="copy-path" data-clipboard-text="原文: http://yoursite.com/2016/05/27/一道常被人轻视的前端JS面试题/　　作者: liyuming" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script src="/js/clipboard.min.js"></script>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" title="中国大陆 (CC BY-NC-SA 3.0 CN)" target = "_blank">"署名-非商用-相同方式共享 3.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



<nav id="article-nav">
  
    <a href="/2016/05/27/chengxuyuan1md/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption"><</strong>
      <div class="article-nav-title">
        
          程序员爱情观
        
      </div>
    </a>
  
  
    <a href="/2016/05/26/chengxuyuan/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">如何成为一个优秀的码农</div>
      <strong class="article-nav-caption">></strong>
    </a>
  
</nav>

  
</article>

    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一道常被人轻视的前端JS面试题"><span class="toc-number">1.</span> <span class="toc-text">一道常被人轻视的前端JS面试题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#第一问"><span class="toc-number">1.1.</span> <span class="toc-text">第一问</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#第二问"><span class="toc-number">1.2.</span> <span class="toc-text">第二问</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#变量声明提升"><span class="toc-number">1.3.</span> <span class="toc-text">变量声明提升</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#函数表达式"><span class="toc-number">1.4.</span> <span class="toc-text">函数表达式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#第三问"><span class="toc-number">1.5.</span> <span class="toc-text">第三问</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#第四问"><span class="toc-number">1.6.</span> <span class="toc-text">第四问</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#第五问"><span class="toc-number">1.7.</span> <span class="toc-text">第五问</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#第六问"><span class="toc-number">1.8.</span> <span class="toc-text">第六问</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#构造函数的返回值"><span class="toc-number">2.</span> <span class="toc-text">构造函数的返回值</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#第七问"><span class="toc-number">2.1.</span> <span class="toc-text">第七问</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#最后"><span class="toc-number">2.2.</span> <span class="toc-text">最后</span></a></li></ol></li></ol>
</div>
<input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

<script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script>
<script>
    var valueHide = "隐藏目录";
    var valueShow = "显示目录";

    if ($(".left-col").is(":hidden")) {
        $("#tocButton").attr("value", valueShow);
    }
    $("#tocButton").click(function() {
        if ($("#toc").is(":hidden")) {
            $("#tocButton").attr("value", valueHide);
            $("#toc").slideDown(320);
        }
        else {
            $("#tocButton").attr("value", valueShow);
            $("#toc").slideUp(350);
        }
    })
    if ($(".toc").length < 1) {
        $("#toc, #tocButton").hide();
    }
</script>





<div class="bdsharebuttonbox">
	<a href="#" class="fx fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
	<a href="#" class="fx fa-weixin bds_weixin" data-cmd="weixin" title="分享到微信"></a>
	<a href="#" class="fx fa-qq bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
	<a href="#" class="fx fa-facebook-official bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
	<a href="#" class="fx fa-twitter bds_twi" data-cmd="twi" title="分享到Twitter"></a>
	<a href="#" class="fx fa-linkedin bds_linkedin" data-cmd="linkedin" title="分享到linkedin"></a>
	<a href="#" class="fx fa-files-o bds_copy" data-cmd="copy" title="分享到复制网址"></a>
</div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>




    
      <div class="duoshuo" id="comments">
    <!-- 多说评论框 start -->
    <div class="ds-thread" data-thread-key="2016/05/27/一道常被人轻视的前端JS面试题/" data-title="JS篇" data-url="http://yoursite.com/2016/05/27/一道常被人轻视的前端JS面试题/"></div>
    <!-- 多说评论框 end -->
    <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
    <script type="text/javascript">
    var duoshuoQuery = {short_name:"null"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = '/js/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
    <!-- 多说公共JS代码 end -->
</div>

    



    <div class="scroll" id="post-nav-button">
        
            <a href="/2016/05/27/chengxuyuan1md/" title="上一篇: 程序员爱情观">
                <i class="fa fa-angle-left"></i>
            </a>
        
        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>
        
            <a href="/2016/05/26/chengxuyuan/" title="下一篇: 如何成为一个优秀的码农">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>
    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2016/05/28/Web前端面试题目汇总/">前端面试题</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/27/chengxuyuan1md/">程序员爱情观</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/27/一道常被人轻视的前端JS面试题/">JS篇</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/26/chengxuyuan/">如何成为一个优秀的码农</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/26/无标题Markdown/">程序员之路</a></li></ul>
    <script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script>
    <script>
        $(".post-list").addClass("toc-article");
        $(".post-list-item a").attr("target","_blank");
        $("#post-nav-button > a:nth-child(2)").click(function() {
            $(".fa-bars, .fa-times").toggle();
            $(".post-list").toggle(300);
            if ($(".toc").length > 0) {
                $("#toc, #tocButton").toggle(200, function() {
                    if ($(".switch-area").is(":visible")) {
                        $("#tocButton").attr("value", valueHide);
                        }
                    })
            }
            else {
            }
        })
    </script>



    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                &copy; 2016 liyuming
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/luuman/hexo-theme-spfk" target="_blank">spfk</a> by luuman
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" >海贼到访数: 
                            <span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>, </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit">本页阅读量: 
                            <span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>

    </div>
    <script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script>
<script src="/js/main.js"></script>

    <script>
        $(document).ready(function() {
            var backgroundnum = 24;
            var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
            $("#mobile-nav").css({"background-image": backgroundimg,"background-size": "cover","background-position": "center"});
            $(".left-col").css({"background-image": backgroundimg,"background-size": "cover","background-position": "center"});
        })
    </script>





<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments"><i class="fa fa-comments-o"></i></a>
    <a href="#footer"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    $(document).ready(function() {
        if ($("#comments").length < 1) {
            $("#scroll > a:nth-child(2)").hide();
        };
    })
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

  <script language="javascript">
    $(function() {
        $("a[title]").each(function() {
            var a = $(this);
            var title = a.attr('title');
            if (title == undefined || title == "") return;
            a.data('title', title).removeAttr('title').hover(

            function() {
                var offset = a.offset();
                $("<div id=\"anchortitlecontainer\"></div>").appendTo($("body")).html(title).css({
                    top: offset.top - a.outerHeight() - 15,
                    left: offset.left + a.outerWidth()/2 + 1
                }).fadeIn(function() {
                    var pop = $(this);
                    setTimeout(function() {
                        pop.remove();
                    }, pop.text().length * 800);
                });
            }, function() {
                $("#anchortitlecontainer").remove();
            });
        });
    });
</script>


  </div>
</body>
</html>